﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UserData
{
    public class SqlServerHelper
    {
        private static SqlServerHelper instance = null;
        private static object o = new object();
        private SqlServerHelper()
        {
        }
        public static SqlServerHelper Instance
        {
            get
            {
                lock (o)
                {
                    if (instance == null)
                    {
                        instance = new SqlServerHelper();
                    }
                    return instance;
                }

            }
        }

        public string GetState(设备前后名称 name)
        {
            #region 1.用代码连接数据库
            //第一种连接，sql server的Windows身份验证
            // string ConStr = "server=192.168.43.7;database=StudentStatusDB;Trusted_Connection=SSPI";
            //第二种连接，sql server身份验证
            string ConStr = "server=192.168.100.250;database=MES;uid=sa;pwd=123;Connect Timeout=1;";
            // string name = "镜头锁付_设备状态";
            string sql = $"SELECT {name.ToString()} FROM [MachineState]";
            string result = "";
            try
            {
                using (SqlConnection conn = new SqlConnection(ConStr))
                {
                    SqlCommand command = new SqlCommand(sql, conn);
                    conn.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    reader.Read();
                    result = reader.GetString(0);
                    reader?.Dispose();
                }
            }
            catch (Exception ex)
            {
                result = "";
            }
            return result;
            #endregion
        }
        public bool SetState(设备前后名称 name, 物料状态 State)
        {
            #region 1.用代码连接数据库
            //第一种连接，sql server的Windows身份验证
            // string ConStr = "server=192.168.43.7;database=StudentStatusDB;Trusted_Connection=SSPI";
            //第二种连接，sql server身份验证
            string ConStr = "server=192.168.100.250;database=MES;uid=sa;pwd=123;Connect Timeout=1;";
            // string name = "镜头锁付_设备状态";
            string sql = $"UPDATE MachineState SET {name.ToString()} = '{State.ToString()}'";
            try
            {
                using (SqlConnection conn = new SqlConnection(ConStr))
                {
                    SqlCommand command = new SqlCommand(sql, conn);
                    conn.Open();
                    SqlDataReader reader = command.ExecuteReader();
                }
                Thread.Sleep(20);
                if (GetState(name) == State.ToString())
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
            #endregion
        }
        public bool SetMachineState(设备前后名称 name, 设备运行状态 State)
        {
            #region 1.用代码连接数据库
            //第一种连接，sql server的Windows身份验证
            // string ConStr = "server=192.168.43.7;database=StudentStatusDB;Trusted_Connection=SSPI";
            //第二种连接，sql server身份验证
            string ConStr = "server=192.168.100.250;database=MES;uid=sa;pwd=123;Connect Timeout=1;";
            // string name = "镜头锁付_设备状态";
            string sql = $"UPDATE MachineState SET {name.ToString()} = '{State.ToString()}'";
            try
            {
                using (SqlConnection conn = new SqlConnection(ConStr))
                {
                    SqlCommand command = new SqlCommand(sql, conn);
                    conn.Open();
                    SqlDataReader reader = command.ExecuteReader();
                }
                Thread.Sleep(20);
                if (GetState(name) == State.ToString())
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
            #endregion
        }

    }
    public enum 设备前后名称
    {
        前壳锁付_设备状态,
        前壳锁付_前面进料状态,
        前壳锁付_前面回流状态,
        前壳锁付_后面出料状态,
        前壳锁付_后面回流状态,
        镜头锁付_设备状态,
        镜头锁付_前面进料状态,
        镜头锁付_前面回流状态,
        镜头锁付_后面出料状态,
        镜头锁付_后面回流状态,
        Plasma_设备状态,
        Plasma_前面进料状态,
        Plasma_前面回流状态,
        Plasma_后面出料状态,
        Plasma_后面回流状态,
        AA_设备状态,
        AA_前面进料状态,
        AA_前面回流状态,
        AA_后面出料状态,
        AA_后面回流状态,
        固化_设备状态,
        固化_前面进料状态,
        固化_前面回流状态,
        固化_后面出料状态,
        固化_后面回流状态,
    }
    public enum 设备运行状态
    {
        空闲,
        回零,
        报警,
        运行,
        异常,
    }
    public enum 物料状态
    {
        未知,
        待料,
        上料完成,
        忙碌,
        等待出料,
        出料完成,
        出OK料,
        出NG料
    }
}
